package gdr.internet;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;

public class Server implements Runnable{
    ArrayList<ObjectOutputStream> clientstreams = new ArrayList<ObjectOutputStream>();
    ServerSocket mySocket;
    
    public Server(int port){
    	System.out.println("INIT");
    	try {
            mySocket = new ServerSocket(port);
            run();
        } catch (IOException e) {
            System.out.println("Unable to start.");
            e.printStackTrace();
        }
    }

    public void run() {
    	System.out.println("RUN");
        while(true){
            try {
                System.out.println("Ready to receive");
                Socket client = mySocket.accept();
            	//clientstreams.add(client);
            	System.out.println(client.getInetAddress().getHostAddress()+" connected to the Server");
            	System.out.println("Server active connection: "+clientstreams.size());
                
                Thread t = new Thread(new ClientHandler(client));
                t.start();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public void shareToAll(Object objectToSchare){
        for(ObjectOutputStream stream:clientstreams){
            try {
                stream.writeObject(objectToSchare);
                stream.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    private class ClientHandler implements Runnable{
        Socket clientSocket;

        public ClientHandler(Socket clientSocket){
            this.clientSocket = clientSocket;
        }
        @Override
        public void run() {
            try {	int cont = 0;
            		String name = "";
            		ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
            		ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
	            	try{
	            	oos.writeObject("messaggio1111");
            		System.out.println(ois.readObject());
            		}
	            	catch (Exception e){e.printStackTrace();}
	            	while(true){
	                    try {Object o = ois.readObject();
	                    	System.out.println("> "+o);
	                    	if (cont==0) {
	                    		name = (String)o;
	                    		System.out.println("Hi! you are "+name);
	                    	}
	                    	else System.out.println(">> "+name+": "+o);
	                    	oos.writeObject("<><><>"+o);
	                    }
	                    catch (Exception e){ break;} //in caso di errore, esce
                }
            }catch(SocketException e){}catch (IOException e) {}
        	clientstreams.remove(clientSocket);
            System.out.println("Update server active connection: "+clientstreams.size());
        }
    }
}